// pages/jielong/activedetail/activedetail.js
var app = getApp();//引用app.js
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var baseURL1 = app.data.baseURL;
var jielong = app.data.jielong;
var jlpay1 = app.data.jlway;
var jlpay = app.data.jlpay;
var page = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "navTab": ["全部", "待收货", "已收货"],
    'status': -1,
    "currentNavtab": "0",
    onclose_cover: false,
    array: ['手机号', '昵称', '姓名'],
    condition: '',
    cvalue: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.id
    })
  },
  hexiao: function (e) {
    console.log(e)
    var that = this;
    var gid = [];
    var areaid = e.currentTarget.dataset.areaid;
    var index = e.currentTarget.dataset.index;
    var orderno = e.currentTarget.dataset.orderno;
    for (var i = 0; i < this.data.data[index].goods.length; i++) {
      gid.push(this.data.data[index].goods[i].id)
    }
    console.log(gid)
    wx.request({
      url: commonURL + 'op=SolitaireWrite',
      data: {
        openid: app.data.openid,
        uniacid: app.data.GZHid,
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        gid: gid,
        orderno: orderno,
        areaid: areaid
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '核销成功',
            complete: function () {
              that.onShow()
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '核销失败',
            showCancel: false,
            complete: function (res) {
              wx.navigateBack({})
            }
          })
        }
      }
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = Number(e.detail.value) + 1
    this.setData({
      index: e.detail.value,
      selectindex: index,
      condition: this.data.array[e.detail.value]
    })
  },
  getnotice: function (e) {
    var that = this;
    wx.request({
      url: jlpay + 'op=singleArriveNotice',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        orderid: e.currentTarget.dataset.id
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          wx.showModal({
            title: '提示',
            content: '通知成功',
            complete: function (res) {
              that.onShow();
            }
          })
        }
      }
    })
  },
  getallnotice: function (e) {
    var that = this;
    if (wx.getStorageSync('tzInfo')) {
      var areaid = wx.getStorageSync('tzInfo').area_id
    }
    wx.request({
      url: jlpay + 'op=arriveAllNotice',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        activeid: this.data.id,
        areaid: areaid
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.showModal({
            title: '提示',
            content: '通知成功',
            complete: function (res) {
              that.onShow();
            }
          })
        }
      }
    })
  },
  switchTab: function (e) {
    var that = this;
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx,
      condition: '',
      cvalue: ''
    });
    var item = e.currentTarget.dataset.idx;

    var status = "-1";
    console.log(that.data.currentNavtab)
    switch (that.data.currentNavtab) {
      case 0:
        status = ""; break;
      case 1:
        status = "2"; break;
      case 2:
        status = "3"; break;
    }
    that.setData({
      status: status
    })
    console.log(status)
    that.getData(Number(this.data.id), status, '', '', '', 5);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData(Number(this.data.id), '', this.data.selectindex, '', '', 5)
  },
  search: function () {
    this.getData(Number(this.data.id), '', this.data.selectindex, this.data.cvalue, '', 5)
  },
  searchchange: function (e) {
    this.setData({
      cvalue: e.detail.value
    })
  },
  getData: function (activiteid, status, condition, cvalue, page, pagesize) {
    var that = this;
    wx.request({
      url: jlpay1 + 'op=getActiveOrders',
      data: {
        activiteid: activiteid,
        statu: status,
        condition: condition,
        cvalue: cvalue,
        page: page,
        pagesize: pagesize
      },
      success: function (res) {
        that.setData({
          data: res.data.data
        })
      }
    })
  },
  onReachBottom: function () {
    var that = this;
    var status = 0;
    switch (that.data.currentNavtab) {
      case "0":
        status = ""; break;
      case "1":
        status = "0"; break;
      case "2":
        status = "8"; break;
      case "3":
        status = "2"; break;
      case "4":
        status = "3"; break;
    }
    page++;
    // this.getData(Number(this.data.id), status, '', '', 0, 10)
    wx.request({
      url: jlpay1 + 'op=getActiveOrders',
      data: {
        activiteid: this.data.id,
        statu: status,
        condition: this.data.selectindex,
        cvalue: this.data.cvalue,
        page: page,
        pagesize: 5
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        var data = res.data.data;
        if (data.length < 1) {
          wx.showToast({
            title: '全部订单加载完成',
            icon: 'success',
            duration: 1000
          })
          return false;
        }
        wx.showToast({
          title: '订单加载中...',
          icon: 'loading',
          duration: 1000
        })

        // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
        // 上拉时需把total在原来的基础上加上count，代表从count条后的数据开始请求。
        that.setData({
          data: that.data.data.concat(res.data.data)
        });
      }
    })
  },
})